// ActionScript file
package 
{
		import flash.events.Event;
		import flash.events.EventDispatcher;
		import flash.net.URLLoader;
		import flash.net.URLRequest;
		
		import mx.controls.Alert;
		import mx.controls.Button;
		import mx.controls.Text;
		import mx.core.Application;
		import mx.core.UIComponent;
		import mx.managers.PopUpManager;
		
		public class ServiceFunctions extends EventDispatcher
		{
			private var iXMLArr:XML;
			private var iLoader:URLLoader;
			private var iSdkArr:Array;
			private var iSdkSolve:Array;
			
			private var iSaveRecForm:SaveRecordsForm;
			
			public var iTable:GenerateTable;
			
			private var score:int;
			
			public function ServiceFunctions(s:GenerateTable = null)
			{
				trace("ENTER:ServiceFunctions()");

				trace("EXIT:ServiceFunctions()");
			}
			
			public function GetScore(event:Event):void
			{
				trace("ENTER:GetScore()");
				score = event.target.now;
				trace("EXIT:GetScore()");
			}
			
			public function CheckNick(src:Text):Boolean
			{
				
				return true;
			}
			
			public function SendRecords():void
			{
				trace("ENTER:SendRecords()");
				
				var tLoader:URLLoader = new URLLoader();
				tLoader.addEventListener("complete", xmlParseScores);
				tLoader.load(new URLRequest("http://www.code-bg.com/alex/UserStats.xml"));
				
				function xmlParseScores(event:Event):void
				{
					trace("ENTER:xmlParseScores()");
					
					iXMLArr = XML(tLoader.data);
					
					trace(iXMLArr);
					
					var temp:String;
					temp = iXMLArr.Player;
					if (temp.indexOf(String(iSaveRecForm.Nickname.text)) != -1 && iSaveRecForm.Nickname.text == "")
					{
						Alert.show("This nickname already exists or the field is empty.");
						return;
					}
					
					var request:URLRequest = new URLRequest();
					request.url = "http://www.code-bg.com/alex/webService.php?name=" + 
					iSaveRecForm.Nickname.text + 
					"&score=" + score;
					
					var loader:URLLoader = new URLLoader ();
					
					loader.addEventListener("complete", CompletecloseSaveRecForm);
					
					loader.load(request);
					
					trace("EXIT:xmlParseScores()");
				}
				trace("EXIT:SendRecords()");
			}
			
			public function CompletecloseSaveRecForm(event:Event):void
			{
				Alert.show("Scores saved.");
				iSaveRecForm.visible = false;
			} 
			
			public function CloseSaveRecForm(event:Event):void
			{
				iSaveRecForm.visible = false;
			} 
			
			public function CallSaveRecForm(aSave:Button):void
			{
				aSave.enabled=false;
				trace("ENTER:CallSaveRecForm()");
				iSaveRecForm = PopUpManager.createPopUp( UIComponent( Application.application ), SaveRecordsForm, true ) as SaveRecordsForm;
				trace(iSaveRecForm);
				PopUpManager.centerPopUp( iSaveRecForm );
				iSaveRecForm.service = this;
				trace("EXIT:CallSaveRecForm()");
			}
			
			public function xmlParse():void
			{
				trace("ENTER:xmlParse()");
				iLoader = new URLLoader();
				iLoader.addEventListener("complete", xmlLoaded);

				iLoader.load(new URLRequest("http://www.code-bg.com/alex/SudokuArr.xml"));

				trace("EXIT:xmlParse()");
			}
			
			public function xmlLoaded(event:Event):void
			{
				trace("ENTER:xmlLoaded()");
				iXMLArr = new XML();
				iXMLArr = XML(iLoader.data);
				ParseXML();
				dispatchEvent(new Event('SrvLoad'));
				trace("EXIT:xmlLoaded()");
			}
			
			public function GetSudokuXML():XML
			{
				return iXMLArr;
			}
			
			public function GetSudoku():Array
			{
				return iSdkArr;
			}
			
			public function GetSdkSolve():Array
			{
				return iSdkSolve;
			}
			
			public function ParseXML():void
			{
				trace('ENTER:ParseXML()');
				var i:int;
				var temp:String = iXMLArr.table;
				trace(iXMLArr);
				iSdkArr = new Array();
				iSdkArr = temp.split( "|" );
				
				for (i = 0; i < 81; i++)
				{
					trace(int(iSdkArr[i]));
				}
				
				temp = iXMLArr.solution;
				iSdkSolve = temp.split( "|" );
				trace('iSdkSolve');
				for (i = 0; i < 81; i++)
				{
					trace(int(iSdkSolve[i]));
				}
				trace('EXIT:ParseXML()');
			}
		}
}